package org.kukker.view;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.kukker.list.KukkerListView;

import android.content.Context;
import android.content.Resources;
import android.graphics.Color;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;


public final class SearchEventsLayout extends FrameLayout {

	public final static String namespace = "org.kukker";
	
	public static SearchEventsLayout instance;

	private String mSearchTerm;
	private String mSearchLocation;
	
	
	private void init(){
		
	}
	
	
	
    /**
     * Construct object, initializing with any attributes we understand from a
     * layout file. These attributes are defined in
     * SDK/assets/res/any/classes.xml.
     * 
     * @see android.view.View#View(android.content.Context, android.util.AttributeSet, java.util.Map)
     */
    public SearchEventsLayout(Context context, AttributeSet attrs, Map inflateParams) {
        super(context, attrs, inflateParams);
        
       // String contextPackageName = this.getContext().getPackageName();
       // String contextClassName = this.getContext().getClass().getName();
        // Class clazz = this.getContext().getClass();
        // clazz.cast(obj);
        
        //Log.i("debug3","context package name: "+contextPackageName+", context class name: "+contextClassName);
        
        SearchEventsLayout.instance = this;
        setClickable(false);//setClickable(true) so that onKeyUp will be invoked whenever key activity occurs.
        setFocusable(false); //setFocusable(true) so that onFocusChanged is invoked whenever focus changes.
        //setBackgroundColor(Color.WHITE);
    	this.setBackgroundColor(Color.BLACK);
    	
    	
    	Log.i("debug3","EventLayout");
    	for(Iterator it=inflateParams.keySet().iterator();it.hasNext();){
    		Object item = it.next();
    		//Log.i("debug3",item.getClass().getSimpleName());
    		
    		try{
    		 Log.i("debug3",
    	    			"["+this.getClass().getSimpleName()+"]" +
    	    			
    	    			"["+
    	    			"]"
    	    			
    	    			+"["
    	    			+"inflateParams "    			
    	    			+", label:"+item.toString()
    	    			+", value:"+inflateParams.get(item.toString()).toString()
    	    			
    	    			+"]"
    	    			
    			 );
    		}catch(Exception e){}
    		
    	}
    	
    	
    	for(int num =attrs.getAttributeCount(),i=0;i<num;i++){
    		String name =attrs.getAttributeName(i);
    		String value = attrs.getAttributeValue(namespace,name);
    		
    		try{
    		 Log.i("debug3",
    	    			"["+this.getClass().getSimpleName()+"]" +
    	    			
    	    			"["+
    	    			"]"
    	    			
    	    			+"["
    	    			+"attrs "    			
    	    			+", name:"+name
    	    			+", value:"+value
    	    			
    	    			+"]"
    	    			
    			 );
    		}catch(Exception e){}
    		
    	}    	
    }
 
    public static SearchEventsLayout getInstance(){
    	return SearchEventsLayout.instance;    	
    }
    
    public boolean onTouchEvent(MotionEvent event){
    	boolean handled = super.onTouchEvent(event);
    	String actionName = org.kukker.Util.kMotionEvent.getActionName(event);
    	 Log.i("eventflow1",
	    			"["+this.getClass().getSimpleName()+"]" +
	    			
	    			"["+
	    			"isSelected:"+this.isSelected()+
	    			"]"
	    			
	    			+"["
	    			+"onTouchEvent()"
	    			
	    			+", action:"+actionName
	    			+"]"
	    			
			 );
    	 return true;
    	
    }
    
    
    public void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect){
    	
    	Log.i("eventflow1",
    			"["+this.getClass().getSimpleName()+"]" +
    			
    			"["+
    			"isSelected:"+this.isSelected()+
    			"]"
    			
    			+"["
    			+"onFocusChanged()"
    			+", gainFocus:"+gainFocus
    			+", direction:"+direction
    			+"]"    			
    	);
	
	if(!gainFocus){
		//EventListView.getInstance().requestFocus();
		
	}
	
	/*
    	//immediately transfer focus to EventListView 
    	if(!gainFocus){
    		this.clearFocus();
    		this.setSelected(false);
    		//EventListView.getInstance().setSelected(true);
    		//EventListView.getInstance().requestFocus();	
    	}    	
    */	
    	
    }
    
    public boolean onKeyUp(int keyCode, KeyEvent event){
    	Log.i("eventflow1",
    			
    			
    			"["+this.getClass().getSimpleName()+"]" +
    			
    			"["+
    			"isSelected:"+this.isSelected()+
    			"]"
    			
    			+"["
    			+"onKeyUp()" +
    			",keyCode:"+keyCode
    			+"]"
    			
    	);
    	
    	
    	OnFocusChangeListener ofcl = new OnFocusChangeListener(){

			@Override
			public void onFocusChanged(View arg0, boolean arg1) {
				// TODO Auto-generated method stub
				
			}
    		
    	};
      	
      	
    	
    	KukkerListView.getInstance().requestFocus();
//    	this.clearFocus();

    	
    	
    	return true;
    }
    
    public boolean onKeyDown(int keyCode, KeyEvent event){
    	//Log.i("eventflow1",this.getClass().getSimpleName()+" isSelected:"+this.isSelected()+", onKeyDown(), "+keyCode);
    	
    	
    	Log.i("eventflow1",
    			
    			
    			"["+this.getClass().getSimpleName()+"]" +
    			
    			"["+
    			"isSelected:"+this.isSelected()+
    			"]"
    			
    			+"["
    			+"onKeyDown(), keyCode:"+keyCode
    			+"]"
    			
    	);
    	

    	this.setFocusable(false);
    	View aView = this.focusSearch(this, View.FOCUS_DOWN);
    	if(aView != null){
    		aView.requestFocus();
    	}
    	
    	//EventListView.getInstance().setSelected(true);
    	//this.clearFocus(); //give up focus
    	//this.setSelected(false);
    	
    	
    	
    	//EventListView.getInstance().requestFocus();
    	//this.clearFocus(); //give up focus
    	
    	
    	
    	return true;
    }
    


}